﻿using System;
using System.IO;
using Antlr4.Runtime;
using Antlr4.Runtime.Tree;

namespace Peter.Code.ECMAScript
{
   /// <summary>
   /// The code analyzer for ecma script.
   /// </summary>
   public class ECMAScriptAnalyzer
   {
      /// <summary>
      /// Analyzes the given data.
      /// </summary>
      /// <param name="data">The data to analyze</param>
      /// <param name="isFile">True if the data is a path to a file, otherwise false.</param>
      public void Analyze (string data, bool isFile = false)
      {
         //StreamReader inputStream = new StreamReader (Console.OpenStandardInput ());
         AntlrInputStream input = new AntlrInputStream (isFile ? File.ReadAllText (data) : data);
         var lexer = new ECMAScriptLexer (input);
         var tokens = new CommonTokenStream (lexer);
         var parser = new ECMAScriptParser (tokens);
         var tree = parser.program ();
         Console.WriteLine (tree.ToStringTree (parser));
         var visitor = new ECMAScriptPeterVisitor();
         Console.WriteLine (visitor.Visit (tree));
         var listener = new ECMAScriptPeterListener ();
         var walker = new ParseTreeWalker ();
         walker.Walk(listener, tree);
      }
   }
}
